﻿using sdkrtv2;
using System.Collections.Generic;

namespace XiaoweiV2
{
    public static class TypeHelper
    {
        public static Skill SkillConvert(TXCA_PARAM_SKILL_CS param_skill)
        {
            Skill skill;
            skill.Id = param_skill.id;
            skill.Name = param_skill.name;
            skill.Type = (SkillType)param_skill.type;

            return skill;
        }

        public static TXCA_PARAM_SKILL_CS SkillConvert(Skill skill)
        {
            TXCA_PARAM_SKILL_CS param_skill;
            param_skill.id = skill.Id;
            param_skill.name = skill.Name;
            param_skill.type = (int)skill.Type;

            return param_skill;
        }

        public static TXCA_PARAM_STATE_CS DeviceStateConvert(DeviceState state)
        {
            TXCA_PARAM_STATE_CS param_state = new TXCA_PARAM_STATE_CS();
            param_state.Setskill_info(SkillConvert(state.Skill));
            param_state.Setplay_state((uint)state.PlayState);
            param_state.Setavailability((uint)state.Availability);
            param_state.Setplay_id(state.PlayId);
            param_state.Setplay_content(state.PlayContent);

            param_state.Setplay_offset(state.PlayOffset);
            param_state.Setplay_mode((uint)state.PlayMode);
            param_state.Setresource_type((uint)state.ResourceType);
            param_state.Setresource_name(state.ResouceName);
            param_state.Setperformer(state.Performer);

            param_state.Setcollection(state.Collection);
            param_state.Setcover_url(state.CoverUrl);
            param_state.Setvolume(state.Volume);
            param_state.Setbrightness(state.Brightness);

            return param_state;
        }

        public static DeviceState DeviceStateConvert(TXCA_PARAM_STATE_CS param_state)
        {
            DeviceState device_state;
            device_state.Skill = SkillConvert(param_state.Getskill_info());
            device_state.PlayState = (PlayState)param_state.Getplay_state();
            device_state.Availability = (DeviceAvailability)param_state.Getavailability();
            device_state.PlayId = param_state.Getplay_id();
            device_state.PlayContent = param_state.Getplay_content();

            device_state.PlayOffset = param_state.Getplay_offset();
            device_state.PlayMode = (PlayMode)param_state.Getplay_mode();
            device_state.ResourceType = (ResourceFormat)param_state.Getresource_type();
            device_state.ResouceName = param_state.Getresource_name();
            device_state.Performer = param_state.Getperformer();

            device_state.Collection = param_state.Getcollection();
            device_state.CoverUrl = param_state.Getcover_url();
            device_state.Volume = param_state.Getvolume();
            device_state.Brightness = param_state.Getbrightness();

            return device_state;
        }

        public static Context ContextConvert(TXCA_PARAM_CONTEXT_CS res)
        {
            if (res == null) return null;
            Context ctx = new Context();
            ctx.Id = res.Getid();
            ctx.SpeakTimeout = res.Getspeak_timeout();
            ctx.SilentTimeout = res.Getsilent_timeout();
            ctx.VoiceRequestBegin = res.Getvoice_request_begin();
            ctx.VoiceRequestEnd = res.Getvoice_request_end();
            ctx.WakeupProfile = (WakeupProfile)res.Getwakeup_profile();
            ctx.WakeupType = (WakeupType)res.Getwakeup_type();
            ctx.WakeupWord = res.Getwakeup_word();
            ctx.RequestParam = res.Getrequest_param();
            ctx.LocalTtsVersion = res.Getlocal_tts_version();
            ctx.Skill = SkillConvert(res.Getskill_info());
            return ctx;
        }

        public static Resource ResourceConvert(TXCA_PARAM_RESOURCE_CS res)
        {
            if (res == null) return null;
            Resource resource = new Resource();
            resource.Format = (ResourceFormat)res.Getformat();
            resource.Id = res.Getid();
            resource.Content = res.Getcontent();
            resource.PlayCount = res.Getplay_count();
            resource.Offset = res.Getoffset();
            resource.ExtendBuffer = res.Getextend_buffer();
            return resource;
        }

        public static ResourceGroup ResourceGroupConvert(TXCA_PARAM_RES_GROUP_CS res_group)
        {
            if (res_group == null) return null;
            ResourceGroup group = new ResourceGroup();
            group.Resources = new List<Resource>();
            foreach(var resource in res_group.GetResource())
            {
                var res = ResourceConvert(resource);
                if(res != null)
                {
                    group.Resources.Add(res);
                }
            }

            return group;
        }

        public static IList<ResourceGroup> ResourceGroupsConvert(IList<TXCA_PARAM_RES_GROUP_CS> groups)
        {
            if (groups == null) return null;

            var resourceGroups = new List<ResourceGroup>();
            foreach (var g in groups)
            {
                resourceGroups.Add(ResourceGroupConvert(g));
            }
            return resourceGroups;
        }


        public static ResponseParam ResponseParamConvert(TXCA_PARAM_RESPONSE_CS res)
        {
            ResponseParam param;
            param.Skill = SkillConvert(res.GetSkillInfo());
            param.LastSkill = SkillConvert(res.GetLastSkillInfo());
            param.ErrorCode = res.GetErrorCode();
            param.VoiceId = res.GetVoiceId();
            param.Context = ContextConvert(res.Getcontext());
            param.RequestText = res.Getrequest_text();
            param.ResponseType =(ResponseType)res.Getresponse_type();
            param.ResponseData = res.Getresponse_data();
            param.IntentInfo = res.Getintent_info();
            param.ResourceGroups = ResourceGroupsConvert(res.Getresource_groups());
            param.HasMorePlaylist = res.Gethas_more_playlist();
            param.HasHistoryPlaylist = res.Gethas_history_playlist();
            param.IsRecovery = res.Getis_recovery();
            param.IsNotify = res.Getis_notify();
            param.WakeupFlag = (WakeupFlag)res.Getwakeup_flag();
            param.PlayBehavior = (PlaylistAction)res.Getplay_behavior();
            param.ResourceListType = (PlaylistType)res.Getresource_list_type();
            param.ResponseText = res.Getrsp_text();
            param.ControlId = (CommandId)res.Getcontrol_id();
            param.ControlValue = res.Getcontrol_value();
            param.HasMorePlaylistUp = res.Gethas_more_playlist_up();

            return param;
        }
    }
}
